/**
 * @author cwoodard
 */
	
// master data structure 
var caterersAndMenus = new Array();
 
function getCaterersAndMenusForBranch( branch_number )
{
    dojo.xhrGet( {
        url:'/index/caterersandmenus/branch_number/'+branch_number,
        handleAs: 'json',
		sync:true,
        load: function(caterers) {
			
			caterersAndMenus = caterers;
			
			// now load caterers
			var catererDropDown = document.getElementById('caterer');
			catererDropDown.length = 0;
			catererDropDown.options[catererDropDown.length] = new Option('Choose a caterer', 'Choose a caterer');
			for( var i=0; i < caterersAndMenus.length; i++ )
			{
				var caterer = caterersAndMenus[i];
				var str = caterer.name;
				catererDropDown.options[catererDropDown.length] = new Option(caterer.name, caterer.name);
			}
			
			var menuDropDown = document.getElementById('menu');
			menuDropDown.length = 0;
			menuDropDown.options[menuDropDown.length] = new Option('Choose a menu', 'Choose a menu');
        }
    } );
}

function selectCatererAndMenu( caterer, menu )
{
	selectCaterer(caterer);
	selectMenu(menu);
}

function selectCaterer( caterer_name )
{
	var catererDropDown = document.getElementById('caterer');
	if(catererDropDown)
	{
		// search catererDropDown for caterer_name
		for( var i=0; i < catererDropDown.options.length; i++ )
		{
			console.log('comparing :'+caterer_name+'" to "'+catererDropDown.options[i].value+'"');
			// if found, get index and set that index as the selected one
			if(catererDropDown.options[i].value == caterer_name)
			{
				console.log('matches');
				catererDropDown.selectedIndex = i;
				
				// now that we've selected the caterer, load the menus
				loadMenusList();
			}
		}
	}	
}

function selectMenu( menu_name )
{
	var menuDropDown = document.getElementById('menu');
	if(menuDropDown)
	{
		// search menuDropDown for menu_name
		for( var i=0; i < menuDropDown.options.length; i++ )
		{
			var pieces = menuDropDown.options[i].value.split(' : ');
			// if found, get index and set that index as the selected one
			if(pieces[0] == menu_name)
			{
				menuDropDown.selectedIndex = i;
				menuWasSelected();
			}
		}
	}
}

function catererWasSelected()
{
	// 1. set menu to "Select a Menu"
	// 2. set cost to 0 and total to 0	
}

function updateTotal( units, price )
{
	var fCost = document.getElementById('cost');
	var fTotal = document.getElementById('total');
	var fHeadcount = document.getElementById('headcount');
	
	fCost.value = price;
	if(fHeadcount.value == '') fHeadcount.value = 0;
	
	if (units == "person") 
	{
		fTotal.value = fCost.value * fHeadcount.value;
	}
	else
	{
		fTotal.value = fCost.value;
	}
}

function menuWasSelected()
{
	var catererDropDown = document.getElementById('caterer');
	var menuDropDown = document.getElementById('menu');
	var selectedCatererIdx = catererDropDown.selectedIndex-1;
	var menus = caterersAndMenus[selectedCatererIdx].menus;
	var selectedMenuIdx = menuDropDown.selectedIndex-1;
	var price = menus[selectedMenuIdx].price;
	var units = menus[selectedMenuIdx].units;	
	var menudetail = document.getElementById('menudetail');
	menudetail.value = menus[selectedMenuIdx].description;
	updateTotal( units, price );
}

function headcountChanged()
{
	// if they change the headcount, recompute the total.
	var catererDropDown = document.getElementById('caterer');
	var menuDropDown = document.getElementById('menu');
	var selectedCatererIdx = catererDropDown.selectedIndex-1;
	var menus = caterersAndMenus[selectedCatererIdx].menus;
	var selectedMenuIdx = menuDropDown.selectedIndex-1;
	var price = menus[selectedMenuIdx].price;
	var units = menus[selectedMenuIdx].units;	
	
	updateTotal( units, price );
}

function loadMenusList()
{
	var catererDropDown = document.getElementById('caterer');
	var menuDropDown = document.getElementById('menu');
	var selectedCatererIdx = catererDropDown.selectedIndex-1;
	var menus = caterersAndMenus[selectedCatererIdx].menus;
		
	// remove the existing menus
	menuDropDown.length = 0;

	menuDropDown.options[menuDropDown.length] = new Option('Choose a menu', 'Choose a menu');
	for (var i = 0; i < menus.length; i++) {
		var str = '';
		str = str + menus[i].name + ' : ';
		str = str + menus[i].price + ' / ' + menus[i].units;
		menuDropDown.options[menuDropDown.length] = new Option(str, str);
	}
}

dojo.addOnLoad( function() { 
		getCaterersAndMenusForBranch(current_branch); 
		// now reselect based on the stored caterer and menu
		selectCatererAndMenu(stored_caterer,stored_menu);
	} 
	);
